package com.edu.internship.busi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.internship.busi.dto.DictionaryOptionDTO;
import com.edu.internship.busi.entity.DictionaryOption;
import com.edu.internship.glabal.exception.BusinessException;
import com.edu.internship.busi.mapper.DictionaryOptionMapper;
import com.edu.internship.busi.service.IDictionaryOptionService;
import com.edu.internship.util.BeanExUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * code is far away from bug with the animal protecting
 *
 * @author lishiquan
 * @description <p>DictionaryOption 的serviceImpl</p>
 * @date 2020-07-06
 */
@Service
@AllArgsConstructor
public class DictionaryOptionServiceImpl extends ServiceImpl<DictionaryOptionMapper, DictionaryOption> implements IDictionaryOptionService {

    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public void createDictionaryOption(DictionaryOptionDTO dto) {
        DictionaryOption dictionaryOption = BeanExUtils.convertTo(dto, DictionaryOption::new);
        super.save(dictionaryOption);
    }

    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public void updateDictionaryOption(DictionaryOptionDTO dto) {
        DictionaryOption dictionaryOption = BeanExUtils.convertTo(dto, DictionaryOption::new);
        super.updateById(dictionaryOption);
    }

    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public void removeDictionaryOption(List<String> ids) {
        super.removeByIds(ids);
    }

    @Override
    public List<DictionaryOption> selectByDictId(String dictId) {
        LambdaQueryWrapper<DictionaryOption> qw = Wrappers.lambdaQuery();
        qw.eq(DictionaryOption::getDictId, dictId);
        return super.list(qw);
    }

    @Override
    public void deleteDictionaryOptionByDictId(String dictId) {
        LambdaQueryWrapper<DictionaryOption> qw = Wrappers.lambdaQuery();
        qw.eq(DictionaryOption::getDictId, dictId);
        super.remove(qw);
    }
}